package com.atguigu.udmp.web.userprofile.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.udmp.web.userprofile.bean.TagInfo;
import com.atguigu.udmp.web.userprofile.bean.TaskInfo;
import com.atguigu.udmp.web.userprofile.service.TagInfoService;
import com.atguigu.udmp.web.userprofile.service.TaskInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-10
 */
@RestController
@RequestMapping("/userprofile/task")
@CrossOrigin
public class TaskInfoController {


    @Autowired
    TaskInfoService taskInfoService;

    @Autowired
    TagInfoService tagInfoService;

    @PostMapping("/detail")
    public String saveTaskInfo(@RequestBody TaskInfo taskInfo){
        taskInfoService.saveOrUpdate(taskInfo);
        return "success";
    }

    @GetMapping("/detail/{id}")
    public String getTaskInfo(@PathVariable("id") Long taskId){
        TaskInfo taskInfo =  taskInfoService.getById(taskId);
        TagInfo tagInfo = tagInfoService.getById(taskInfo.getTagId());
        taskInfo.setTagName(tagInfo.getTagName());
        taskInfo.setTagCode(tagInfo.getTagCode());
        taskInfo.setTagValueType(tagInfo.getTagValueType());

        return JSON.toJSONString(taskInfo);
    }



    @DeleteMapping("/detail/{taskId}")
    @CrossOrigin
    public String deleteTask(@PathVariable("taskId") Long taskId){
        taskInfoService.removeById(taskId);
        return "success";
    }

}
